package com.sokal.easycare.modules.sysparams.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sokal.easycare.common.persistence.Page;
import com.sokal.easycare.common.service.CrudService;
import com.sokal.easycare.common.utils.StringUtils;
import com.sokal.easycare.modules.sysparams.dao.SysParamsDao;
import com.sokal.easycare.modules.sysparams.entity.SysParams;

/**
 * 参数配置，用于配置系统中统一格式参数内容Service
 * @author lihaipei
 * @version 2017-04-18
 */
@Service
@Transactional(readOnly = true)
public class SysParamsService extends CrudService<SysParamsDao, SysParams> {

	@Autowired
	private static SysParamsDao sysParamsDao;

	public SysParams get(String id) {
		return super.get(id);
	}
	
	public List<SysParams> findList(SysParams sysParams) {
		return super.findList(sysParams);
	}
	
	public Page<SysParams> findPage(Page<SysParams> page, SysParams sysParams) {
		return super.findPage(page, sysParams);
	}
	
	@Transactional(readOnly = false)
	public void save(SysParams sysParams) {
		super.save(sysParams);
	}
	
	@Transactional(readOnly = false)
	public void delete(SysParams sysParams) {
		super.delete(sysParams);
	}
	
	public Map<String, Object> checkValue(SysParams sysParams) {
		Map<String, Object> map = new HashMap<String, Object>();
		
		if (StringUtils.isNotBlank(sysParams.getParamType())) {
			SysParams param0 = new SysParams();
			param0.setParamType(sysParams.getParamType());
			param0.setId(sysParams.getId());
			List<SysParams> param0s = super.findList(param0);
			if (!param0s.isEmpty()) {
				map.put("result", false);
				map.put("msg", "参数类型已存在！");
			}
			if (isNumeric(sysParams.getParamValue())) {
				if(Double.parseDouble(sysParams.getParamValue()) >1){
					map.put("result", false);
					map.put("msg", "分成比例已超出总百分比！");
				}
			} else {
				map.put("result", false);
				map.put("msg", "参数值请输入数字格式！");
			}
		}
		
		else if (StringUtils.isNotBlank(sysParams.getParamCode())) {
			if (SysParams.PARAM_CODE1_.equals(sysParams.getParamCode())) {
				SysParams param0 = new SysParams();
				param0.setParamName(sysParams.getParamName());
				param0.setParamCode(sysParams.getParamCode());
				param0.setId(sysParams.getId());
				List<SysParams> param0s = super.findList(param0);
				if (!param0s.isEmpty()) {
					map.put("result", false);
					map.put("msg", "类型分配角色已存在！");
				} else {
					SysParams param1 = new SysParams();
					param1.setParamCode(sysParams.getParamCode());
					param1.setId(sysParams.getId());
					List<SysParams> param1s = super.findList(param1);
					double sumPer = 0.0d;
					for (SysParams sysParam : param1s) {
						sumPer += Double.parseDouble(sysParam.getParamValue());
					}
					sumPer = sumPer + Double.parseDouble(sysParams.getParamValue());
					if (sumPer > 1) {
						map.put("result", false);
						map.put("msg", "分成比例已超出总比例！");
					}
				}
			}
		} 
		
		return map;
	}
	
	/**
	 * 判断是否为数字(正负数都行)
	 * @author lihaipei
	 * @date 2017年4月19日 下午5:20:11
	 * @param str 需要验证的字符串
	 * @return
	 */
	private static boolean isNumeric(String str) { 
		Pattern pattern = Pattern.compile("-?[0-9]*.?[0-9]*");
		Matcher isNum = pattern.matcher(str);
		if( !isNum.matches() ){
	           return false; 
		} 
		return true; 
	}

	/**
	 * 在系统参数中，根据参数名获取参数值
	 * @param _parameter
	 * @return
	 */
	public static String getValueByName(String _parameter) {
		String valueByName = sysParamsDao.getValueByName(_parameter);
		return valueByName;
	}
}